<template>
  <Dialog v-model="adialogDialog" title="追加外呼号码" class="append-dialog">
    <div class="dialog-content">
      <!-- 头部标题 -->
      <div class="dialog-header">
        <div class="header-icon">
          <Icon icon="ep:plus" />
        </div>
        <div class="header-content">
          <h3 class="dialog-title">追加外呼号码</h3>
          <p class="dialog-subtitle">选择导入方式添加外呼号码</p>
        </div>
      </div>

      <!-- 标签页 -->
      <div class="tabs-container">
        <el-tabs v-model="activeName" @tab-click="handleClick" class="enhanced-tabs">
          <el-tab-pane name="call-file">
            <template #label>
              <div class="tab-label">
                <Icon icon="ep:upload" class="tab-icon" />
                <span>导入文件</span>
              </div>
            </template>
          </el-tab-pane>
          <el-tab-pane name="call-task">
            <template #label>
              <div class="tab-label">
                <Icon icon="ep:list" class="tab-icon" />
                <span>从呼叫名单</span>
              </div>
            </template>
          </el-tab-pane>
        </el-tabs>
      </div>

      <!-- 表单内容 -->
      <div class="form-container">
        <el-form ref="formRef" :model="formData" label-width="120px" class="enhanced-form">
          <!-- 文件上传 -->
          <div v-if="activeName == 'call-file'" class="upload-section">
            <el-form-item label="上传文件" :required="true" class="form-item">
              <div class="upload-wrapper">
                <div class="upload-area">
                  <el-upload
                    ref="uploadRef"
                    class="upload-component"
                    v-model:file-list="fileList"
                    :auto-upload="false"
                    :limit="1"
                    accept=".txt,.xlsx"
                    action="none"
                    drag
                  >
                    <div class="upload-content">
                      <Icon icon="ep:upload-filled" class="upload-icon" />
                      <div class="upload-text">
                        <p class="upload-title">点击或拖拽文件到此处上传</p>
                        <p class="upload-desc">支持 .txt 和 .xlsx 格式</p>
                      </div>
                    </div>
                  </el-upload>
                </div>

                <div class="template-section">
                  <div class="template-header">
                    <Icon icon="ep:document" class="template-icon" />
                    <span class="template-title">下载模板</span>
                  </div>
                  <div class="template-buttons">
                    <el-button size="small" @click="downTem" class="template-btn">
                      <Icon icon="ep:download" class="btn-icon" />
                      Excel模板
                    </el-button>
                  </div>
                </div>
              </div>
            </el-form-item>

            <!-- 提示信息 -->
            <div class="tips-section">
              <div class="tips-header">
                <Icon icon="ep:info-filled" class="tips-icon" />
                <span class="tips-title">温馨提示</span>
              </div>
              <div class="tips-content">
                <div class="tip-item">
                  <Icon icon="ep:check" class="tip-icon" />
                  <span class="tip-text">Excel表格文件的第一行不会被导入</span>
                </div>
                <div class="tip-item">
                  <Icon icon="ep:check" class="tip-icon" />
                  <span class="tip-text">单次导入名单数量建议不超过10W条，过多容易导入失败</span>
                </div>
              </div>
            </div>
          </div>

          <!-- 呼叫名单选择 -->
          <div v-if="activeName == 'call-task'" class="select-section">
            <el-form-item label="选择呼叫名单" required class="form-item">
              <div class="select-wrapper">
                <div class="select-input" @click="selectCallPhone">
                  <div class="select-content">
                    <Icon icon="ep:list" class="select-icon" />
                    <span v-if="selectCallName" class="select-text">{{ selectCallName }}</span>
                    <span v-else class="select-placeholder">请选择呼叫名单</span>
                  </div>
                  <div class="select-actions">
                    <el-button link @click.stop="selectCallPhone" class="action-btn">
                      <Icon icon="ep:edit" />
                    </el-button>
                    <el-button link @click.stop="addCall" class="action-btn add-btn">
                      <Icon icon="ep:plus" />
                    </el-button>
                  </div>
                </div>
              </div>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="adialogDialog = false" class="cancel-btn">
          <Icon icon="ep:close" class="btn-icon" />
          取消
        </el-button>
        <el-button @click="submitForm" type="primary" :disabled="formLoading" class="confirm-btn">
          <Icon icon="ep:check" class="btn-icon" />
          确定
        </el-button>
      </div>
    </template>
  </Dialog>

  <CallSelects ref="callRefs" @changeCalle="changeCalle" />
  <callListForm ref="callListRefs" />
</template>

<script setup>
import { EditPen, Plus } from '@element-plus/icons-vue'
import * as taskApi from '@/api/call/task/index'
import { RecordApi } from '@/api/call/record'
import calleeListTemplete from './callList/calleeListTemplete.txt'
import callListForm from './callList/callListForm.vue'
import CallSelects from './call.vue'
import axios from 'axios'
import download from '@/utils/download'
const activeName = ref('call-file')
const selectCallName = ref(null)
const adialogDialog = ref(false)
const fileList = ref([])
const formData = reactive({
  thirdPartyTaskId: null,
  number: null,
  callTaskCalleeListId: null
})
/*显示*/
const open = (id) => {
  adialogDialog.value = true
  formData.thirdPartyTaskId = id
  formData.number = null
  fileList.value = []
  formData.callTaskCalleeListId = null
  selectCallName.value = null
}
const callListRefs = ref(null)
/*新增名单*/
const addCall = () => {
  callListRefs.value.open('create')
}
defineExpose({ open })
/*下载模板 xlsx*/
const downTem = async () => {
  const data = await RecordApi.getTemplate()
  download.excel(data, '呼叫名单模板.xlsx')
}
/*下载模板 txt*/
const downTxt = async () => {
  const response = await axios({
    url: calleeListTemplete,
    responseType: 'blob'
  })
  download.txt(response.data, '呼叫名单模板.txt')
}
/*切换*/
const handleClick = () => {
  formData.number = null
  fileList.value = []
  formData.callTaskCalleeListId = null
  selectCallName.value = null
}
/*选择名单*/
const changeCalle = (val) => {
  selectCallName.value = val.name
  formData.callTaskCalleeListId = val.id
}
const callRefs = ref(null)
const selectCallPhone = () => {
  callRefs.value.open()
}
const emit = defineEmits(['success'])
const formLoading = ref(false)
/*追加单个号码*/
const appendPhoneOne = async () => {
  const phoneRegex = /^1[3-9]\d{9}$/
  if (!formData.number) {
    ElMessage.error('手机号不能为空')
    return
  }
  try {
    formLoading.value = true
    const formDataUp = new FormData()
    formDataUp.append('thirdPartyTaskId', formData.thirdPartyTaskId)
    formDataUp.append('number', formData.number)

    await taskApi.appendOne(formDataUp)
    ElMessage.success('追加成功')
    adialogDialog.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}
/*追加文件*/
const appendPhoneFile = async () => {
  if (fileList.value.length == 0) {
    ElMessage.error('请上传文件')
    return
  }
  try {
    formLoading.value = true
    const formDataUp = new FormData()
    formDataUp.append('thirdPartyTaskId', formData.thirdPartyTaskId)
    formDataUp.append('file', fileList.value[0].raw)
    await taskApi.appendFile(formDataUp)
    ElMessage.success('追加成功')
    adialogDialog.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}
/*选择文件*/
const appendPhoneTask = async () => {
  if (!formData.callTaskCalleeListId) {
    ElMessage.error('请选择呼叫名单')
    return
  }
  try {
    formLoading.value = true
    const formDataUp = new FormData()
    formDataUp.append('thirdPartyTaskId', formData.thirdPartyTaskId)
    formDataUp.append('callTaskCalleeListId', formData.callTaskCalleeListId)

    await taskApi.appendCalle(formDataUp)
    ElMessage.success('追加成功')
    adialogDialog.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}
const submitForm = async () => {
  if (activeName.value == 'call-file') {
    appendPhoneFile()
  } else {
    appendPhoneTask()
  }
}
</script>

<style lang="scss" scoped>
.append-dialog {
  :deep(.el-dialog) {
    border-radius: 12px;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 20px auto;
    }
  }

  :deep(.el-dialog__header) {
    display: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
    border-top: 1px solid #e5e7eb;
  }
}

.dialog-content {
  background: #f8fafc;
}

/* 对话框头部 */
.dialog-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 24px;
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  color: white;
  border-radius: 10px;

  @media (max-width: 768px) {
    padding: 20px;
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;

  @media (max-width: 768px) {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}

.header-content {
  flex: 1;
}

.dialog-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 18px;
  }
}

.dialog-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 标签页 */
.tabs-container {
  background: white;
  padding: 0 24px;

  @media (max-width: 768px) {
    padding: 0 20px;
  }
}

.enhanced-tabs {
  :deep(.el-tabs__header) {
    margin: 0;
    border-bottom: 2px solid #f1f5f9;
  }

  :deep(.el-tabs__nav-wrap) {
    &::after {
      display: none;
    }
  }

  :deep(.el-tabs__item) {
    padding: 16px 20px;
    font-weight: 500;
    color: #6b7280;
    border-bottom: 2px solid transparent;
    transition: all 0.2s ease;

    &:hover {
      color: #0052cc;
    }

    &.is-active {
      color: #0052cc;
      border-bottom-color: #0052cc;
    }
  }
}

.tab-label {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tab-icon {
  font-size: 16px;
}

/* 表单容器 */
.form-container {
  background: white;
  padding: 24px;

  @media (max-width: 768px) {
    padding: 20px;
  }
}

.enhanced-form {
  .form-item {
    margin-bottom: 24px;

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: #374151;
      font-size: 14px;
    }
  }
}

/* 上传区域 */
.upload-section {
  .upload-wrapper {
    display: flex;
    flex-direction: column;
    gap: 20px;
  }

  .upload-area {
    border: 2px dashed #d1d5db;
    border-radius: 8px;
    transition: all 0.2s ease;

    &:hover {
      border-color: #0052cc;
    }
  }

  .upload-component {
    :deep(.el-upload) {
      width: 100%;
    }

    :deep(.el-upload-dragger) {
      width: 100%;
      height: 120px;
      border: none;
      border-radius: 8px;
      background: #f8fafc;
      transition: all 0.2s ease;

      &:hover {
        background: rgba(0, 82, 204, 0.05);
      }
    }
  }

  .upload-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 12px;
  }

  .upload-icon {
    font-size: 32px;
    color: #6b7280;
  }

  .upload-text {
    text-align: center;
  }

  .upload-title {
    font-size: 16px;
    font-weight: 500;
    color: #374151;
    margin: 0 0 4px 0;
  }

  .upload-desc {
    font-size: 14px;
    color: #6b7280;
    margin: 0;
  }
}

/* 模板区域 */
.template-section {
  background: #f8fafc;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 16px;
}

.template-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.template-icon {
  color: #0052cc;
  font-size: 16px;
}

.template-title {
  font-size: 14px;
  font-weight: 500;
  color: #374151;
}

.template-buttons {
  display: flex;
  gap: 8px;
}

.template-btn {
  border: 1px solid #0052cc;
  color: #0052cc;
  background: white;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 12px;
  transition: all 0.2s ease;

  &:hover {
    background: rgba(0, 82, 204, 0.05);
  }

  .btn-icon {
    margin-right: 4px;
  }
}

/* 提示区域 */
.tips-section {
  background: linear-gradient(135deg, #e6f3ff 0%, #f0f8ff 100%);
  border: 1px solid #b3d9ff;
  border-radius: 8px;
  padding: 16px;
  margin-top: 16px;
}

.tips-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.tips-icon {
  color: #0052cc;
  font-size: 16px;
}

.tips-title {
  font-size: 14px;
  font-weight: 600;
  color: #0052cc;
}

.tips-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tip-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.tip-icon {
  color: #10b981;
  font-size: 14px;
  margin-top: 2px;
  flex-shrink: 0;
}

.tip-text {
  font-size: 13px;
  color: #374151;
  line-height: 1.4;
}

/* 选择区域 */
.select-section {
  .select-wrapper {
    width: 100%;
  }

  .select-input {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    background: white;
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: 40px;

    &:hover {
      border-color: #0052cc;
      box-shadow: 0 2px 8px rgba(0, 82, 204, 0.1);
    }
  }

  .select-content {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    flex: 1;
  }

  .select-icon {
    color: #6b7280;
    font-size: 16px;
    flex-shrink: 0;
  }

  .select-text {
    color: #374151;
    font-weight: 500;
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .select-placeholder {
    color: #9ca3af;
    font-size: 14px;
  }

  .select-actions {
    display: flex;
    border-left: 1px solid #e5e7eb;
  }

  .action-btn {
    padding: 8px 12px;
    color: #6b7280;
    border-radius: 0;
    transition: all 0.2s ease;

    &:hover {
      color: #0052cc;
      background: rgba(0, 82, 204, 0.05);
    }

    &.add-btn {
      border-left: 1px solid #e5e7eb;
      color: #10b981;

      &:hover {
        color: #059669;
        background: rgba(16, 185, 129, 0.05);
      }
    }
  }
}

/* 对话框底部 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  background: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
  }
}

.cancel-btn {
  border: 2px solid #e5e7eb;
  color: #6b7280;
  background: white;
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.2s ease;

  &:hover {
    border-color: #d1d5db;
    color: #374151;
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.confirm-btn {
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  border: none;
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
  color: white;
  transition: all 0.2s ease;

  &:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  &:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 上传列表样式 */
:deep(.el-upload-list) {
  margin-top: 12px;

  .el-upload-list__item {
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    background: #f8fafc;
    margin-top: 8px;
    transition: all 0.2s ease;

    &:hover {
      background: #f1f5f9;
    }
  }

  .el-upload-list__item-file-name {
    color: #374151;
    font-weight: 500;
  }

  .el-upload-list__item-status-label {
    color: #10b981;
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .dialog-content {
    background: #1f2937;
  }

  .tabs-container,
  .form-container,
  .dialog-footer {
    background: #374151;
  }

  .dialog-title {
    color: #f9fafb;
  }

  .enhanced-tabs {
    :deep(.el-tabs__header) {
      border-bottom-color: #4b5563;
    }

    :deep(.el-tabs__item) {
      color: #d1d5db;

      &:hover,
      &.is-active {
        color: #60a5fa;
      }

      &.is-active {
        border-bottom-color: #60a5fa;
      }
    }
  }

  .upload-area {
    border-color: #4b5563;

    &:hover {
      border-color: #60a5fa;
    }
  }

  .upload-component {
    :deep(.el-upload-dragger) {
      background: #4b5563;

      &:hover {
        background: rgba(96, 165, 250, 0.1);
      }
    }
  }

  .upload-title {
    color: #f9fafb;
  }

  .upload-desc,
  .upload-icon {
    color: #d1d5db;
  }

  .template-section {
    background: #4b5563;
    border-color: #6b7280;
  }

  .template-title {
    color: #f9fafb;
  }

  .tips-section {
    background: rgba(96, 165, 250, 0.1);
    border-color: rgba(96, 165, 250, 0.3);
  }

  .tips-title {
    color: #60a5fa;
  }

  .tip-text {
    color: #d1d5db;
  }

  .select-input {
    background: #4b5563;
    border-color: #6b7280;

    &:hover {
      border-color: #60a5fa;
    }
  }

  .select-text {
    color: #f9fafb;
  }

  .select-placeholder {
    color: #9ca3af;
  }

  .select-actions {
    border-left-color: #6b7280;
  }

  .action-btn {
    color: #d1d5db;

    &:hover {
      color: #60a5fa;
      background: rgba(96, 165, 250, 0.1);
    }

    &.add-btn {
      border-left-color: #6b7280;
      color: #34d399;

      &:hover {
        color: #10b981;
        background: rgba(52, 211, 153, 0.1);
      }
    }
  }
}
</style>
